#include <jsoncpp/json/json.h>
#include "Vec.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <string.h>

using namespace std;

void write()
{
    Json::Value root;
    Json::Value recgrid;
    Json::Value circle;
    root["description"] = "ExcludedDisk";
    recgrid["lowerleft"].append(0);
    recgrid["lowerleft"].append(0);
    recgrid["highright"].append(1);
    recgrid["highright"].append(1);
    recgrid["boxsize"].append(64);
    recgrid["boxsize"].append(64);
    root["recgrid"]=recgrid;

    circle["center"].append(0.2);
    circle["center"].append(0.5);
    circle["radius"]=0.25;
    root["circle"]=circle;

    root["tolerance"]=1e-15;

    Json::StyledWriter writer;
    ofstream outjson;
    outjson.open("first.json");
    outjson << writer.write(root);
    outjson.close();
    return;
}


void read()
{
    ifstream infile("first.json");
    assert(infile.good());
    Json::Reader read;
    Json::Value root;
    if(!read.parse(infile,root))
    {
        cout<<"Error when reading\n";
        return;
    }
    Json::Value circle;
 
    PGET(root,circle);
    Json::Value description;
    PGET(root,description);
    string name = description.asCString();
    cout<<name<<'\n';
    auto center = circle["center"]; 


    Vec<Real,2> c111{root["center"][0].asDouble(),root["center"][1].asDouble()};
    Vec<Real,2> c{(*center.begin()).asDouble(),(*(++center.begin())).asDouble()};
    cout<< c111;
    auto &tolerance=root["tolerance"];
   // PGET(root,tolerance);
    Real tol = root["tolerance"].asDouble();
    cout<<tol;
    return;
}
int main()
{
    write();
    read();
}